﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using FlashCards.SL.FlashCardRepeater.DataModels;
using FlashCards.Dto;
using System.Collections.ObjectModel;
using FlashCards.SL.FlashCardRepeater.ViewModels.Commands;
using System.Linq;
using FlashCards.SL.FlashCardRepeater.Utils;

namespace FlashCards.SL.FlashCardRepeater.ViewModels
{
    public class RepeatSettingsViewModel : BaseViewModel
    {
        public List<DifficultyLevel> DifficultyLevels { get; set; }
        public FlashcardSet FlashcardSet { get; set; }
        public List<int> FlashcardIds { get; set; }

        List<RepeatItem> _lastRepeats;

        private int _selectedFlashcardsCount;
        public int SelectedFlashcardsCount
        {
            get { return _selectedFlashcardsCount; }
            set { _selectedFlashcardsCount = value; OnPropertyChanged("SelectedFlashcardsCount"); }
        }

        public bool IsRandomOrderEnabled { get; set; }

        public event EventHandler<ConfigurationCompletedEventArgs> OnConfigurationCompleted;

        public ObservableCollection<Flashcard> SelectedFlashcards { get; set; }

        public RepeatSettingsViewModel(FlashcardSet set, List<RepeatItem> repeats)
        {
            DifficultyLevels = new List<DifficultyLevel>();
            DifficultyLevels.Add(new DifficultyLevel(0, "New", false));
            DifficultyLevels.Add(new DifficultyLevel(1, "Poor", false));
            DifficultyLevels.Add(new DifficultyLevel(2, "Average", false));
            DifficultyLevels.Add(new DifficultyLevel(3, "Good", false));

            _lastRepeats = repeats;
            FlashcardSet = set;
            InitializeCommands();
        }

        #region COMMANDS

        public ICommand SelectFlashcardsCommand { get; private set; }
        public ICommand StartRepeatCommand { get; private set; }

        void InitializeCommands()
        {
            SelectFlashcardsCommand = new Command(SelectFlashcards);
            StartRepeatCommand = new Command(StartRepeat, CanStartRepeat);
        }

        void SelectFlashcards()
        {
            var levels = DifficultyLevels.Where(x => x.IsChecked).Select(x => x.Value).ToList();

            FlashcardIds = _lastRepeats.Where(x => levels.Contains(x.RepeatMark)).Select(x => x.FlashcardId).ToList();

            //if (DifficultyLevels.FirstOrDefault(x => x.Value == 0).IsChecked)
            //{
            //    var newFlashcardsIds = new List<int>();
            //    //foreach (var item in FlashcardSet.Flashcards.Where(x => x.LastMark == null))
            //    //{
            //    //    newFlashcardsIds.Add(item.Id);
            //    //}

            //    FlashcardIds.AddRange(newFlashcardsIds);
            //}

            SelectedFlashcardsCount = FlashcardIds.Count;
            StartRepeatCommand.CanExecute(null);
            (StartRepeatCommand as Command).RaiseCanExecuteChanged();
        }

        void StartRepeat()
        {
            if (OnConfigurationCompleted != null)
            {
                var selectorType = typeof(BasicFlashcardSelector);
                if (IsRandomOrderEnabled)
                    selectorType = typeof(RandomFlashcardSelector);

                OnConfigurationCompleted.Invoke(this, new ConfigurationCompletedEventArgs(FlashcardIds, selectorType));
            }
        }

        bool CanStartRepeat()
        {
            return SelectedFlashcardsCount > 0;
        }

        #endregion
    }

    public class ConfigurationCompletedEventArgs : EventArgs
    {
        public List<int> FlashcardsIds { get; set; }
        public Type FlashcardsSelectorType { get; set; }

        public ConfigurationCompletedEventArgs(List<int> flashcardsIds, Type selectorType)
        {
            FlashcardsIds = flashcardsIds;
            FlashcardsSelectorType = selectorType;
        }
    }
}
